/**
 * Created by joy on 12/30/16.
 */
import request from 'restler'

export default class RequestHelper {
  get (url) {
    return new Promise((resolve, reject) => {
      request.get(url, {
        timeout: 10000
      }).on('success', function (data, res) {
        resolve({
          data: data,
          res: res
        })
        this.abort()
      }).on('failed', (data, res) => {
        reject({
          msg: `${url} ${res.statusCode}`
        })
      }).on('error', (err, res) => {
        reject({
          msg: `${url}访问失败`,
          ...err
        })
      }).on('timeout', (ms) => {
        reject({
          msg: `${url}访问超时`
        })
      }).on('complete', function (result) {
        if (!this.aborted) {
          reject({
            msg: `${url} error`
          })
        }
      })
    })
  }

  getJson (url, headers = {}, params = {}) {
    return new Promise((resolve, reject) => {
      if (Object.keys(params).length > 0) {
        url = url + '?'
        for (let key in params) {
          let val = params[key]
          url = url + key + '=' + val + '&'
        }
        url = url.substring(0, url.length - 1)
      }
      request.get(url, {
        timeout: 10000,
        headers: headers || {
          'Content-Type': 'application/json; charset=utf-8'
        }
      }).on('success', function (data, res) {
        resolve(JSON.parse(data))
        this.abort()
      }).on('failed', (data, res) => {
        reject({
          msg: `${url} ${res.statusCode}`
        })
      }).on('error', (err, res) => {
        reject({
          msg: `${url}访问失败`,
          ...err
        })
      }).on('timeout', (ms) => {
        reject({
          msg: `${url}访问超时`
        })
      }).on('complete', function (result) {
        if (!this.aborted) {
          reject({
            msg: `${url} error`
          })
        }
      })
    })
  }

  post (url, headers = {}, params = {}) {
    return new Promise((resolve, reject) => {
      request.post(url, {
        timeout: 10000,
        data: params,
        headers: headers || {
          'Content-Type': 'application/json; charset=utf-8'
        }
      }).on('success', function (data, res) {
        resolve({
          data: data,
          res: res
        })
        this.abort()
      }).on('failed', (data, res) => {
        reject({
          msg: `${url} ${res.statusCode}`
        })
      }).on('error', (err, res) => {
        reject({
          msg: `${url}访问失败`,
          ...err
        })
      }).on('timeout', (ms) => {
        reject({
          msg: `${url}访问超时`
        })
      }).on('complete', function (result) {
        if (!this.aborted) {
          reject({
            msg: `${url} error`
          })
        }
      })
    })
  }

  postJson (url, headers = {}, params = {}) {
    return new Promise((resolve, reject) => {
      request.postJson(url, params, {
        timeout: 10000,
        headers: headers || {
          'Content-Type': 'application/json; charset=utf-8'
        }
      }).on('success', function (data, res) {
        resolve(data)
        this.abort()
      }).on('failed', (data, res) => {
        reject({
          msg: `${url} ${res.statusCode}`
        })
      }).on('error', (err, res) => {
        reject({
          msg: `${url}访问失败`,
          ...err
        })
      }).on('timeout', (ms) => {
        reject({
          msg: `${url}访问超时`
        })
      }).on('complete', function (result, res) {
        if (!this.aborted) {
          reject({
            msg: `${url} error`
          })
        }
      })
    })
  }

  postJsonByAuth (url, username, password, params = {}) {
    return new Promise((resolve, reject) => {
      request.postJson(url, params, {
        timeout: 10000,
        username: username,
        password: password
      }).on('success', function (data, res) {
        resolve(data)
        this.abort()
      }).on('failed', (data, res) => {
        reject({
          msg: `${url} ${res.statusCode}`
        })
      }).on('error', (err, res) => {
        reject({
          msg: `${url}访问失败`,
          ...err
        })
      }).on('timeout', (ms) => {
        reject({
          msg: `${url}访问超时`
        })
      }).on('complete', function (result, res) {
        if (!this.aborted) {
          reject({
            msg: `${url} error`
          })
        }
      })
    })
  }
}
